﻿using System;
using System.Collections.Generic;
using System.Linq;

using mvcForum.Core;
using mvcForum.Core.Interfaces.Services;
using mvcForum.Core.Specifications;
using mvcForum.Core.Interfaces.Data;

namespace mvcForum.Web.Services {

	public class GroupService : IGroupService {
		private readonly IRepository<Group> groupRepo;
		private readonly IRepository<GroupMember> gmRepo;

		public GroupService(IRepository<Group> groupRepo, IRepository<GroupMember> gmRepo) {
			this.groupRepo = groupRepo;
			this.gmRepo = gmRepo;
		}

		public IEnumerable<Group> GetGroups(ForumUser user) {
			// Let's get all the group memberships the given user has.
			IEnumerable<GroupMember> memberships = this.gmRepo.ReadMany(new GroupMemberSpecifications.SpecificUser(user));
			// Return the groups of the memberships.
			return memberships.Select(x => x.Group).Distinct();
		}
	}
}